﻿



import Request from '../../base/request.js?t=202511041006'
import Storage from '../../base/storage.js?t=202511041006'
import {  queryURLParams, IgetDeptList, getBrowserInfo, previewCom, popShowCom } from "../../base/utils.js?t=202511041006"
const Composition = (proxy) => {
    const data = reactive({
        messageTest: "",
        proxy: "",
        popTitle: "",
        cIndex: 1,
        previewInfo: {
            pdfurl: "",
            pdfOpen: false,
            previewShow: false,
            imgUrl: ""
        },
        uinfo: {

        },

        formData: {
            Place: null,
            LocationDetail: null,
            Contactor: null,
            Phone: null,
            X: null,
            Y: null
        },
        formRules: {
            Place: [
                { required: true, message: '请输入场所', trigger: 'change' }
            ],
            LocationDetail: [
                { required: true, message: '请输入地点', trigger: 'change' }
            ],
            Contactor: [
                { required: true, message: '请输入联系人', trigger: 'change' }
            ],
            //Phone: [
            //    { required: true, message: '请输入号码', trigger: 'change' }
            //],
            X: [
                { required: true, message: '请拾取X轴位置', trigger: 'change' }
            ],
            Y: [
                { required: true, message: '请拾取Y轴位置', trigger: 'change' }
            ],
        },
        tabList: [
            {
                label: '浦东', value: 1
            }, {
                label: '大场', value: 2
            }],
        queryParams: {
      
        },

        urlinfo: {},
        popType:null,
        uploadLock: false,
        opType: true,
        popShow: false,
        pagination: {
            total: 0,
            current: 1,
            pageSize: 10,
            loading: true,
            showTotal: total => `总共 ${total} 项`,
            defaultPageSize: 10,
            showSizeChanger: true, // 是否显示pagesize选择
            showQuickJumper: false, // 是否显示跳转窗
        },
        levelList: [

        ],
        citem: {},
        deptList:[],
        columns: [
            {
                title: '序号',
                dataIndex: 'index',
                key: 'index',
                align: 'center',
                width: 80,
                customRender: ({ index }) => {
                    return (params.pagination.value.current - 1) * params.pagination.value.pageSize + index + 1
                },
            },
            {
                title: '厂区',
                dataIndex: 'FactoryAreaName',
                key: 'FactoryAreaName',
                align: 'center',
       

            },
            {
                title: '地点',
                dataIndex: 'FactoryAreaName',
                key: 'FactoryAreaName',
                align: 'center',
                customRender: ({ record }) => {
                    return `${record.Place}(${record.LocationDetail || '-'})`
                },


            },
            {
                title: '联系人',
                dataIndex: 'FactoryAreaName',
                key: 'FactoryAreaName',
                align: 'center',
                customRender: ({ record }) => {
                    return `${record.Contactor}(${record.Phone||'-'})`
                },

            },
            {
                title: '操作',
                key: 'action',
                align: 'center',
                customRender: function ({ text, record, index, column }) {

                    const FDBtn = h(antd.Popconfirm, {
                        title: `提示：点位【${record.FactoryAreaName}-${record.LocationDetail}】，删除后将无法恢复，请您确认。`,
                        onConfirm: () => methods.handleDelete(record)

                    }, { default: () => [delBtn] })

                    let detailBtn = h(antd.Button, {
                        type: "link",
                        onClick: () => {
                            methods.handleDetail(record)
                        }
                    }, { default: () => '详情' })

                    let editBtn = h(antd.Button, {
                        type: "link",
                        onClick: () => {
                            methods.handleEdit(record)
                        }
                    }, { default: () => '编辑' })


                    let delBtn = h(antd.Button, {
                        type: "link",
                        danger:true
                    }, { default: () => '删除' })
                    return h('div', {

                    },
                        { default: () => [ editBtn,  FDBtn, detailBtn] }
                    );

                }
            },
        ],
        dataSource: [],
    })
    let params = toRefs(data)
    const methods = {
        tchange: () => {
            Storage.set(`${window.location.pathname}Index`, params.cIndex.value, true)
            params.queryParams.value = {
            }
            params.queryParams.value.FactoryAreaId = params.cIndex.value
            methods.getTableList(true)
        },
        getDeptList: () => {
            return new Promise(async (resolve, rej) => {
                params.deptList.value = await IgetDeptList(Request)
                if (params.deptList.value) {
                    resolve(1)
                }
            })


        },
        handleMark: (e) => {
            if (params.popType.value == 9) {
                return
            }
            const { layerX, layerY } = e

            params.formData.value.X = Number(Number(layerX / params.proxy.value.$refs.img.width).toFixed(4) * 100).toFixed(2)
            params.formData.value.Y = Number(Number(layerY / params.proxy.value.$refs.img.height).toFixed(4) * 100).toFixed(2)

        },
        toAdd: () => {
            params.popType.value = null
            params.formData.value = {}
            params.popShow.value=true
        },
        handleOk: () => {
            if (params.popType.value == 9) {
                return params.popShow.value = false
            }
            params.proxy.value.$refs.formRef
                .validate()
                .then(() => {
                    if (params.popType.value == 1) {
                        Request({
                            url: "/EmergencyPlan/EditAED",
                            method: 'post',
                            data: { FactoryAreaId: params.cIndex.value, ...params.formData.value }
                        }).then(res => {
                            params.messageTest.value.success(res.Msg);
                            methods.getTableList(true)
                        }).finally(() => {
                            params.popShow.value = false
                        })
                    } else {
                        Request({
                            url: "/EmergencyPlan/AddAED",
                            method: 'post',
                            data: { FactoryAreaId: params.cIndex.value, ...params.formData.value }
                        }).then(res => {
                            params.messageTest.value.success(res.Msg);
                            methods.getTableList(true)
                        }).finally(() => {
                            params.popShow.value = false
                        })
                    }
                })
                .catch(error => {
                    console.log('error', error);
                });
        },
        handleDetail: (item) => {
            params.popType.value = 9
            params.formData.value = item
            params.popShow.value = true
        },
        handlePreview: (item) => {
            if (item.FileName.indexOf('.pdf') != -1) {
                params.previewInfo.value.pdfUrl = itema.FilePath
                params.previewInfo.value.pdfOpen = true
            } else {
                params.previewInfo.value.imgUrl = item.FilePath
                params.previewInfo.value.previewShow = true
            }
        },

        handleEdit: (item) => {
            params.popType.value = 1
            params.formData.value=item
            params.popShow.value = true
        },

        handleDelete: (item) => {
            Request({
                url: "/EmergencyPlan/DeleteAED",
                method: 'post',
                data: {
                    Id: item.Id
                }
            }).then(res => {
                params.messageTest.value.success(res.Msg);
                methods.getTableList(true)
            })

        },



        handleTableChange: (page) => {
            const { current, pageSize } = page
            params.pagination.value.current = current;
            params.pagination.value.pageSize = pageSize;
            methods.getTableList()
        },
   
        getTableList: async (isRresh) => {
            if (isRresh) {
                params.pagination.value.current = 1
            } 
            params.pagination.value.loading = true
            Request({
                url: "/EmergencyPlan/GetAEDPageList",
                method: 'post',
                params: {
                    page: params.pagination.value.current,
                    pageSize: params.pagination.value.pageSize
                },
                data: {
                    ...params.queryParams.value,
                    FactoryAreaId: params.cIndex.value
                }
            }).then(res => {
                params.dataSource.value = res.Data.Data
                params.pagination.value.total = res.Data?.TotalItemCount
            }).finally(() => {
                params.pagination.value.loading = false
            })
        },
        filterOption: (val, type) => {
            return `${type.label}`.includes(`${val || ''}`)
        },


        init: () => {
            getBrowserInfo().catch(err => { console.log(err) })
            if (proxy) {
                params.proxy.value = proxy
            }
    

            const key = `${window.location.pathname}Index`;
            const storedValue = Storage.get(key, true); // 获取原始值（字符串或 null）

            if (storedValue !== null && storedValue !== undefined && !isNaN(storedValue)) {
                params.cIndex.value = +Storage.get(`${window.location.pathname}Index`, true)
            }
            params.urlinfo.value = queryURLParams()
            const { message } = antd
            params.messageTest.value = message
            params.messageTest.value.config({
                top: `10vh`,
                duration: 7,
                maxCount: 1,
                rtl: true,
            });
            setTimeout(() => {
                let info = Storage.get('AccountInfo', true)
                params.uinfo.value = info
                Request({
                    url: "/Account/createJwtToken",
                    method: 'post',
                    data: {
                        "userName": info && info.UserNo
                    }
                }).then(async res => {
                    Storage.set('token', res.Data, true)
                    methods.getDeptList()
          
                    methods.getTableList(true)
 
                })
            }, 400)

        },
    }

    const template = {
        imageModel: previewCom(params, methods).imageModel,
        pdfPopModel: previewCom(params, methods).pdfPopModel,
    }
    return {
        ...params,
        ...methods,
        template
    }
}

export default Composition